#include"cmd1.h"
/* Usage:  */
static int pathSeparation(char*path,int *size,char**real_path){
    *size=0;
    char *p=path;
        char**q=real_path;
        while(*p==' '){
            p++;
        }
    while(p!=NULL&&*p!=0){
        if(*p=='/'){
            p++;
        }
        else{
            while(*p==' '){
                p++;
            }
            int i=0;
            while(p!=NULL&&*p!=0&&*p!='/'){
                (*q)[i]=*p;
                i++;
                p++;
            }
            q++;
            (*size)++;
        }
    }
    return 0;
}
static int stackPush(char **now_addr,char *dir_name,int *size){
    now_addr[*size]=dir_name;
    (*size)++;
    return 0;
}
static int stackPop(char**now_addr,int *size){
    if((*size)==0){
        printf("in the root dir\n");
        return 1;
    }
    (*size)--;
    char *p=now_addr[*size];
    free(p);
    now_addr[*size]=NULL;
    return 0;
}

int compare(const void *a,const void *b){
    return strcmp(*(char**)a,*(char**)b);
}
int cdFunc(char*username,char* path,int path_size,int *now_path_id,MYSQL *mysql,char*errbuf){
    char now_path[PATHMAX]={0};
    char q[1024]={0};
    if(strcmp(path," ")==0){
    sprintf(q,"select id from vft where path = '/%s' and type= 'd' ;",username);
    mysql_query(mysql,q);
    MYSQL_RES*rows=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(rows);
    *now_path_id=atoi(row[0]);
    return 0;
    }
    sprintf(q,"select path from vft where id=%d and type= 'd';",*now_path_id);
    mysql_query(mysql,q);
    MYSQL_RES*rows =  mysql_store_result(mysql);
    if(rows==NULL){
        sprintf(errbuf,"Can not find this file\n");
        return -1;
    }
    MYSQL_ROW row=mysql_fetch_row(rows);
    memcpy(now_path,row[0],strlen(row[0]));
    getRealPath(path,path_size,now_path);
    bzero(q,strlen(q));
    sprintf(q,"select id from vft where path = '%s' and user= '%s' and type= 'd';",now_path,username);
    mysql_query(mysql,q);
    bzero(&rows,sizeof(rows));
    MYSQL_RES* rows1=mysql_store_result(mysql);
    if(mysql_num_rows(rows1)==0){
        sprintf(errbuf,"Can not find this path\n");
        return -1;
    }
    MYSQL_ROW row1=mysql_fetch_row(rows1);
    *now_path_id=atoi(row1[0]);
        return 0;
}
int lsFunc(char*username,char*path,int path_size,int now_path_id,char*ret_vle,MYSQL *mysql,char*errbuf){
<<<<<<< HEAD
    printf("path=%s\n",path);
    if(strcmp(path," ")==0){
        strcpy(path,".");
        path_size=1;
    }
    printf("path=%s\n",path);
=======
    /* printf("name:%s,path:%s,path_size:%d,id:%d\n",username,path,path_size,now_path_id); */
>>>>>>> origin/master
int res=cdFunc(username,path,path_size,&now_path_id,mysql,errbuf);
    if(res==-1){
        sprintf(errbuf,"Can not find this file\n");
        return -1;
    }
    char p[1024]={0};
    sprintf(p,"select filename from vft where parent_id = %d;",now_path_id);
    mysql_query(mysql,p);
    MYSQL_RES *rows=mysql_store_result(mysql);
    unsigned long filenum=mysql_num_rows(rows);
    MYSQL_ROW row;
    for(unsigned long i=0;i<filenum;i++){
        row=mysql_fetch_row(rows);
        strcat(ret_vle,row[0]);
        strcat(ret_vle,"\n");
    }
    return 0;
}
    
int rmfileFunc(char*username,char*path,int path_size,int now_path_id,MYSQL *mysql,char* file_store_path,char*errbuf){
    if(strcmp(path,".")==0||strcmp(path," ")==0){
        sprintf(errbuf,"path illegal\n");
        return -1;
    }
char now_path[PATHMAX]={0};
    char q[1024]={0};
    sprintf(q,"select path from vft where id=%d;",now_path_id);
    mysql_query(mysql,q);
    MYSQL_RES *rows=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(rows);
    memcpy(now_path,row[0],strlen(row[0]));
    getRealPath(path,path_size,now_path);
    bzero(q,strlen(q));
    sprintf(q,"select md5 from vft where path = '%s' and user ='%s' and type ='f';",now_path,username);
    mysql_query(mysql,q);
    rows=mysql_store_result(mysql);
    if(mysql_num_rows(rows)==0){
        sprintf(errbuf,"Can not find this file\n");
        return -1;
    }
    row=mysql_fetch_row(rows);
    char* md5val=(char*)calloc(1024,sizeof(char));
    md5val=row[0];
    bzero(q,strlen(q));
    sprintf(q,"select count(*) from vft where md5 = '%s';",md5val);
    mysql_query(mysql,q);
    rows=mysql_store_result(mysql);
    row=mysql_fetch_row(rows);
        bzero(q,strlen(q));
        sprintf(q,"delete from vft where path= '%s' and md5 = '%s'and user = '%s';",now_path,md5val,username);
        mysql_query(mysql,q);
        if(atoi(row[0])==1){
            char filepath[1024]={0};
            memcpy(filepath,file_store_path,strlen(file_store_path));
            strcat(filepath,"/");
            strcat(filepath,md5val);
<<<<<<< HEAD
=======
            /* printf("filrpath;%s\n",filepath); */
>>>>>>> origin/master
            int res=remove(filepath);
            if(res!=0){
                return -1;
            }
        }
    return 0;
}

int getRealPath(char*path,int path_size,char*now_addr){
     char**spath=(char**)calloc(path_size,sizeof(char*));
    for(int i=0;i<path_size;i++){
        char*buf=(char*)calloc(1024,sizeof(char));
        spath[i]=buf;
    }
    char**now_spath=(char**)calloc(path_size+strlen(now_addr),sizeof(char*));
    for(int i=0;i<path_size+strlen(now_addr);i++){
        char*buf=(char*)calloc(1024,sizeof(char));
        now_spath[i]=buf;
    }
    int pathsize;
    pathSeparation(path,&pathsize,spath);
    int now_addr_size;
    pathSeparation(now_addr,&now_addr_size,now_spath);
    for(int i=0;i<pathsize;i++){
        if(strcmp(spath[i],"..")==0){
            free(spath[i]);
            stackPop(now_spath,&now_addr_size);
        }
        else if(strcmp(spath[i],".")==0){
            free(spath[i]);
            continue;
        }
        else{
            stackPush(now_spath,spath[i],&now_addr_size);
        }
    }
    bzero(now_addr,strlen(now_addr));
    if(now_addr_size==0){
        memcpy(now_addr,".",1);
        return 0;
    }
    for(int i=0;i<now_addr_size;i++){
        strcat(now_addr,"/");
        strncat(now_addr,now_spath[i],strlen(now_spath[i]));
    }
free(spath);
    for(int i=0;i<now_addr_size;i++){
        free(now_spath[i]);
    }
    free(now_spath);

 return 0;
}
